require('../css/register.less')

document.ready(function () {
    let pawBtn = document.querySelector('#pawBtn');
    //跳转到登录页
    pawBtn.addEventListener('click', function () {
        location.href = './login.html'
    })

    //验证码部分
    let captchaCode = ''; // 当前验证码
    // 创建验证码渲染器
    let captch = document.querySelector('#captch')
    const captchaDrawer = new CaptchaMini({
        lineNum: 8, //线条数量
        fontSize: 30, //字体大小
    });

    captchaDrawer.draw(captch, function (code) {
        captchaCode = code;
    });
    // console.log(captchaCode.toLowerCase());

    //获取dom节点
    let phoneNum = document.querySelector('#phoneNum'); //电话
    let verification = document.querySelector('#verification'); //验证码
    let password = document.querySelector('#password'); //密码
    let agen = document.querySelector('#agen'); //再确认密码
    let registerBtn = document.querySelector('#registerBtn'); //按钮
    let hint = document.querySelector('#hint'); //提示语


    //手机失焦
    // phoneNum.addEventListener('blur', function () {
    //     let phoneNumVal = phoneNum.value; //手机值
    //     //手机
    //     if (phoneNumVal === '') {
    //         hint.textContent = '手机号不能为空！'
    //         return
    //     } else if (!window.utils.isPhoneNumber(phoneNumVal)) {
    //         hint.textContent = '手机号格式错误！'
    //         return;
    //     }
    // })

    //验证码失焦
    // verification.addEventListener('blur', function () {
    //     let verificationVal = verification.value; //验证码值
    //     if (verificationVal === '') {
    //         hint.textContent = '验证码不能为空！'
    //         return
    //     } else if (verificationVal.toLowerCase() != captchaCode.toLowerCase()) {
    //         hint.textContent = '手机号格式错误！'
    //         return;
    //     }
    // })

    //密码失焦
    // password.addEventListener('blur', function () {
    //     let passwordVal = password.value; //密码值
    //     //密码
    //     if (passwordVal === '') {
    //         hint.textContent = '密码不能为空！'
    //         return
    //     } else if (!window.utils.isPassword(passwordVal)) {
    //         hint.textContent = '密码格式错误！'
    //         return;
    //     }
    // })

    //确认密码失焦
    // agen.addEventListener('blur', function () {
    //     let passwordVal = password.value; //密码值
    //     let agenVal = agen.value; //确认密码值 
    //     //确认密码
    //     if (agenVal === '') {
    //         hint.textContent = '密码不能为空！'
    //         return
    //     } else if (agenVal != passwordVal) {
    //         hint.textContent = '两次密码不一致'
    //         return;
    //     }
    // })


    //注册
    registerBtn.addEventListener('click', function () {
        let phoneNumVal = phoneNum.value; //手机值
        let verificationVal = verification.value; //验证码值
        let passwordVal = password.value; //密码值
        let agenVal = agen.value; //确认密码值  

        //手机
        if (phoneNumVal === '') {
            hint.textContent = '手机号不能为空！'
            return
        } else if (!window.utils.isPhoneNumber(phoneNumVal)) {
            hint.textContent = '手机号格式错误！'
            return;
        }

        //验证码
        if (verificationVal === '') {
            hint.textContent = '验证码不能为空！'
            return
        } else if (verificationVal.toLowerCase() != captchaCode.toLowerCase()) {
            hint.textContent = '手机号格式错误！'
            return;
        }

        //密码
        if (passwordVal === '') {
            hint.textContent = '密码不能为空！'
            return
        } else if (!window.utils.isPassword(passwordVal)) {
            hint.textContent = '密码格式错误！'
            return;
        }

        //确认密码
        if (agenVal === '') {
            hint.textContent = '密码不能为空！'
            return
        } else if (agenVal != passwordVal) {
            hint.textContent = '两次密码不一致'
            return;
        }

        //发送请求
        $http.post('/users/add', {
                account: phoneNumVal,
                password: passwordVal,
            },
            function (res) {
                if (res.status === 0) {
                    //发送登录请求
                    // hint.textContent = res.msg
                    window.$http.post('/users/login', {
                            account: phoneNumVal,
                            password: passwordVal,
                        },
                        function (res) {
                            if (res.status === 0) {
                                // 登录成功
                                console.log(res);
                                let obj = {
                                    userId: res.data.user.userId,
                                    account: res.data.user.account,
                                    password: res.data.user.password,
                                    nickname: res.data.user.nickname,
                                    gender: res.data.user.gender,
                                    birthday: res.data.user.birthday,
                                    imgurl: res.data.user.imgurl,
                                    address: res.data.user.address,
                                    sign: res.data.user.sign,
                                    describe: res.data.user.describe,
                                    token: res.data.user.token

                                }
                                localStorage.setItem('user', JSON.stringify(obj))
                                
                                window.utils.toast('tick','注册成功');
                                window.setTimeout(function () {
                                    location.href = './index.html'
                                }, 2000)

                            } else {
                                // 登录失败
                                console.log(res.msg);
                                hint.textContent = res.msg
                                
                            }
                        }
                    )
                } else {
                    // 注册失败
                    // hint.textContent = res.msg;
                    window.utils.toast('fork',res.msg);
                }
            })
    })












})